package com.csu.jpetstore_reconfiguration_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csu.jpetstore_reconfiguration_back.common.CommonResponse;
import com.csu.jpetstore_reconfiguration_back.domain.Order;
import com.csu.jpetstore_reconfiguration_back.domain.Product;
import com.csu.jpetstore_reconfiguration_back.service.ProductService;
import com.csu.jpetstore_reconfiguration_back.mapper.ProductMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 朱嘉辉
* @description 针对表【t_product】的数据库操作Service实现
* @createDate 2024-04-13 20:29:21
*/
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
    implements ProductService{
    @Resource
    private ProductMapper productMapper;

    @Override
    public CommonResponse<List<Product>> getAllProducts()
    {
        List<Product> products = productMapper.selectList(null);
        if(products == null)
        {
            return CommonResponse.createForError("商品为空");
        }
        return CommonResponse.createForSuccess("查询成功", products);
    }

    @Override
    public CommonResponse<Product> getProduct(String id)
    {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id",id);
        Product product = productMapper.selectOne(queryWrapper);
        if (product == null)
        {
            return CommonResponse.createForError("查询失败");
        }
        return CommonResponse.createForSuccess("查询成功",product);
    }

    @Override
    public CommonResponse<Product> addProduct(Product product)
    {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id",product.getProductId());
        Product product1 = productMapper.selectOne(queryWrapper);
        if(product1 != null)
        {
            return CommonResponse.createForError("商品已存在");
        }
        productMapper.insert(product);
        return CommonResponse.createForSuccess("添加成功",product);
    }

    @Override
    public CommonResponse<Product> updateProduct(String id,Product product)
    {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id",id);
        Product product1 = productMapper.selectOne(queryWrapper);
        if(product1 == null)
        {
            return CommonResponse.createForError("商品不存在");
        }
        productMapper.update(product,queryWrapper);
        return CommonResponse.createForSuccess("更新成功",product);
    }

    @Override
    public CommonResponse<Product> deleteProduct(String id)
    {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id",id);
        Product product = productMapper.selectOne(queryWrapper);
        if(product == null)
        {
            return CommonResponse.createForError("商品不存在");
        }
        productMapper.delete(queryWrapper);
        return CommonResponse.createForSuccess("删除成功",product);
    }
}




